package org.jash.demo2205.goods

import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.core.graphics.get
import androidx.core.graphics.set
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.zxing.BarcodeFormat
import com.google.zxing.EncodeHintType
import com.google.zxing.MultiFormatWriter
import com.google.zxing.common.CharacterSetECI
import com.google.zxing.pdf417.encoder.BarcodeMatrix
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel
import com.hyphenate.helpdesk.easeui.util.IntentBuilder
import com.umeng.socialize.ShareAction
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.shareboard.ShareBoard
import com.youth.banner.loader.ImageLoader
import org.jash.demo2205.R
import org.jash.demo2205.databinding.ActivityDetailBinding
import org.jash.demo2205.entity.Goods
import org.jash.demo2205.mvvm.BaseActivity

@Route(path = "/shop/detail")
class DetailActivity : BaseActivity<ActivityDetailBinding, DetailViewModel>() {
    @Autowired
    @JvmField
    var id = 0
    lateinit var goods:Goods
    override fun initData() {
        viewModel.goodsLiveData.observe(this) {
            binding.goods = it
            goods = it
            binding.banner.update(it.bannerList)
        }
        viewModel.cartLiveData.observe(this) {
            Toast.makeText(this, it, Toast.LENGTH_SHORT).show()
        }
        ARouter.getInstance().inject(this)
        viewModel.getDetail(id)

    }

    override fun initView() {
        binding.toolbar.setNavigationOnClickListener {
            finish()
        }
        binding.banner.setImageLoader(object :ImageLoader() {
            override fun displayImage(context: Context?, path: Any?, imageView: ImageView?) {
                imageView?.let {
                    Glide.with(it)
                        .load(path)
                        .into(it)
                }
            }
        })
        binding.addToCart.setOnClickListener {
            viewModel.addToCart(id)
        }
        binding.toolbar.setOnMenuItemClickListener {
            when(it.itemId) {
                R.id.qrcode -> {
                    val encode = MultiFormatWriter().encode(
                        goods.goods_desc,
                        BarcodeFormat.QR_CODE,
                        500,
                        500,
                        mutableMapOf<EncodeHintType, Any>(
                            EncodeHintType.ERROR_CORRECTION to ErrorCorrectionLevel.H,
                            EncodeHintType.CHARACTER_SET to CharacterSetECI.UTF8
                        )
                    )
                    val bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.RGB_565)
                    for (i in 0 until 500) {
                        for(j in 0 until 500) {
                            bitmap[i, j] = if (encode[i, j]) Color.BLACK else Color.WHITE
                        }
                    }
                    val canvas = Canvas(bitmap)
                    val logo =
                        BitmapFactory.decodeResource(resources, R.mipmap.bg)
                    canvas.drawBitmap(logo, Rect(0,0, logo.width, logo.height), Rect(200, 200, 300, 300), Paint())
                    val img = ImageView(this)
                    img.setImageBitmap(bitmap)
                    MaterialAlertDialogBuilder(this)
                        .setView(img)
                        .show()
                }
                R.id.share -> {
                    ShareAction(this).setDisplayList(SHARE_MEDIA.SINA, SHARE_MEDIA.WEIXIN, SHARE_MEDIA.QQ)
                        .withText(goods.goods_desc)
                        .setShareboardclickCallback { _, _ ->  }
                        .open()
                }
            }
            true
        }
        binding.buy.setOnClickListener {
            ARouter.getInstance().build("/shop/pay")
                .withString("info", "${goods.goods_desc} ${goods.goods_default_price}x1")
                .withFloat("total", goods.goods_default_price)
                .navigation()
        }
        binding.agent.setOnClickListener {
            startActivity(IntentBuilder(this).setServiceIMNumber("kefuchannelimid_181577").build())
        }

    }
}